package com.ambitious.service.impl;

import com.ambitious.domain.NormalCourse;
import com.ambitious.domain.WeekCourse;
import com.ambitious.domain.academic.Academic;
import com.ambitious.domain.academic.BasicCourse;
import com.ambitious.domain.academic.CourseInfo;
import com.ambitious.domain.token.EduToken;
import com.ambitious.service.EducationService;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Ambitious
 */
@Service
public class EducationServiceImpl implements EducationService {


    @Override
    public List<WeekCourse> getWeekCourses(List<NormalCourse> courses, int totalWeek) {
        // 1 初始化列表
        WeekCourse[] weekCourses = new WeekCourse[totalWeek];
        for(int i = 0; i < weekCourses.length; i++) {
            weekCourses[i] = new WeekCourse();
            weekCourses[i].setCurWeek(i + 1);
        }
        // 2 遍历每一门课,处理数据
        for (NormalCourse course : courses) {
            String[] weeks = course.getZcd().split(",");
            // 遍历weeks
            for (String week : weeks) {
                // 分两种情况，①："1-16周" ②："13周"
                if(!week.contains("-")) {
                    // ②
                    int inx = Integer.parseInt(week.substring(0, week.indexOf("周")));
                    weekCourses[inx - 1].getCourses().add(course);
                }else {
                    // ①
                    String[] weekNums = week.split("-");
                    int i = Integer.parseInt(weekNums[0]);
                    int j = Integer.parseInt(weekNums[1].substring(0, weekNums[1].indexOf("周")));
                    for(int m = i; m <= j; m++) {
                        weekCourses[m - 1].getCourses().add(course);
                    }
                }
            }
        }
        return new ArrayList<>(Arrays.asList(weekCourses));
    }

    @Override
    public void setLimitCredit(Academic academic, String value) {
        int index;
        double res;
        String basic = "&nbsp;要求学分:";
        // 学科基础课程
        String jc = "学科基础课程" + basic;
        index = value.indexOf(jc);
        res = handleCredit(value.substring(index + jc.length()));
        academic.getJc().setLimitCredit(res);
        // 专业必修课程
        String zybx = "专业必修课程" + basic;
        index = value.indexOf(zybx);
        res = handleCredit(value.substring(index + zybx.length()));
        academic.getZybx().setLimitCredit(res);
        // 集中性实践教学
        String jzx = "集中性实践教学环节" + basic;
        index = value.indexOf(jzx);
        res = handleCredit(value.substring(index + jzx.length()));
        academic.getJzx().setLimitCredit(res);
        // 通识类必修
        String tsbx = "通识类必修课程" + basic;
        index = value.indexOf(tsbx);
        res = handleCredit(value.substring(index + tsbx.length()));
        academic.getTsbx().setLimitCredit(res);
        // 教师教育类必修课程
        String jsjy = "教师教育类必修课程" + basic;
        index = value.indexOf(jsjy);
        res = handleCredit(value.substring(index + jsjy.length()));
        academic.getJsjy().setLimitCredit(res);
        // 方向模块
        String fxmk = "方向模块" + basic;
        index = value.indexOf(fxmk);
        res = handleCredit(value.substring(index + fxmk.length()));
        academic.getFxmk().setLimitCredit(res);
        // 专业选修课程
        String zyxx = "专业选修课程" + basic;
        index = value.indexOf(zyxx);
        res = handleCredit(value.substring(index + zyxx.length()));
        academic.getZyxx().setLimitCredit(res);
        // 创新与创业
        String cxcy = "创新与创业" + basic;
        index = value.indexOf(cxcy);
        res = handleCredit(value.substring(index + cxcy.length()));
        academic.getCxcy().setLimitCredit(res);
        // 历史与文化
        String lswh = "创新与创业" + basic;
        index = value.indexOf(lswh);
        res = handleCredit(value.substring(index + lswh.length()));
        academic.getLswh().setLimitCredit(res);
        // 大学体育5,6
        String dxty = "大学体育5,6" + basic;
        index = value.indexOf(dxty);
        res = handleCredit(value.substring(index + dxty.length()));
        academic.getDxty().setLimitCredit(res);
        // 艺术与审美
        String yssm = "艺术与审美" + basic;
        index = value.indexOf(yssm);
        res = handleCredit(value.substring(index + yssm.length()));
        academic.getYssm().setLimitCredit(res);
        // 精品思政课程
        String jpsz = "精品思政课程" + basic;
        index = value.indexOf(jpsz);
        res = handleCredit(value.substring(index + jpsz.length()));
        academic.getJpsz().setLimitCredit(res);
        // 其他课程
        String qt = "其他课程" + basic;
        index = value.indexOf(qt);
        res = handleCredit(value.substring(index + qt.length()));
        academic.getQt().setLimitCredit(res);
    }

    protected double handleCredit(String value) {
        // 解析学分
        StringBuilder tmp = new StringBuilder("");
        char cur;
        boolean isCredit = false;
        for(int i = 0; i < value.length(); i++) {
            cur = value.charAt(i);
            isCredit = (cur >= '0' && cur <= '9') || cur == '.';
            if(!isCredit) {
                // 不是有效字符
                break;
            }else {
                tmp.append(cur);
            }
        }
        if("".equals(tmp.toString())) {
            // 没有最低学分
            return 0.0;
        }
        return Double.parseDouble(tmp.toString());
    }

    @Override
    public HttpPost initAcademicPost(EduToken eduToken, String url, String value) throws UnsupportedEncodingException {
        HttpPost post = new HttpPost(url);
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("xfyqjd_id",value));
        post.addHeader("Cookie", String.format("JSESSIONID=%s;SF_cookie_18=%s",eduToken.getJSessionId(),eduToken.getSfCookie18()));
        post.setEntity(new UrlEncodedFormEntity(params));
        return post;
    }

    @Override
    public void setCourseInfo(CourseInfo courseInfo, List<BasicCourse> courses) {
        // 创建变量计算已修学分
        double finishCredit = 0;
        // 1 遍历课程列表
        for (BasicCourse course : courses) {
            Integer xdzt = course.getXDZT();
            if(xdzt == 1) {
                // 在修
                courseInfo.getReadingList().add(course);
            }else if(xdzt == 2) {
                // 未过
                courseInfo.getUnPassList().add(course);
            }else if(xdzt == 3) {
                // 未修
                courseInfo.getUnReadingList().add(course);
            }else if(xdzt == 4) {
                // 已修，学分算入已修学分
                courseInfo.getPassList().add(course);
                finishCredit += Double.parseDouble(course.getXF());
            }
        }
        // 2 设置已修学分
        courseInfo.setFinishCredit(finishCredit);
    }
}
